<template>
  <div class="app-container">
    <div>
      <h2>{{book.bookname}}</h2>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="章节名" prop="secname">
        <el-input v-model="queryParams.secname" placeholder="请输入章节名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['book:section:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['book:section:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['book:section:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['book:section:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <!-- <el-table-column label="章节名" align="center" prop="bookname" /> -->
      <el-table-column label="章节名" align="center" prop="secname" />
      <!-- <el-table-column label="内容" align="center" prop="ncontent" /> -->
      <el-table-column label="加入时间" align="center" prop="addtime" width="180">
        <!-- <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addtime, '{y}-{m}-{d} {h}:{mi}:{s}') }}</span>
        </template> -->
      </el-table-column>
      <!-- <el-table-column label="书籍" align="center" prop="bookname" /> -->
      <el-table-column label="顺序" align="center" prop="ordid" />
      <el-table-column label="是否收费" align="center" prop="isfee">

        <template slot-scope="scope">
          <span>{{ scope.row.isfee? '收费':'免费' }}</span>
        </template>

      </el-table-column>
      <!--  <el-table-column label="音频地址" align="center" prop="audiourl" /> -->
      <el-table-column label="音频地址" align="center" prop="audiourl">
        <template slot-scope="scope">
          <audio :src="parseAudio(scope.row.audiourl)" controls></audio> 
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['book:section:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['book:section:remove']">删除</el-button>

          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleGenerateAudio(scope.row)"
            v-hasPermi="['book:section:audio']">生成音频</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改章节对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="章节名" prop="secname">
          <el-input v-model="form.secname" placeholder="请输入章节名" />
        </el-form-item>
        <el-form-item label="序号" prop="ordid">
          <el-input-number v-model="form.ordid" controls-position="right" :min="1" />

        </el-form-item>

        <el-form-item label="内容">
          <editor v-model="form.ncontent" :min-height="192" />
        </el-form-item>
        <!-- <el-form-item label="加入时间" prop="addtime">
          <el-date-picker clearable
            v-model="form.addtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择加入时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="是否收费" prop="isfee">
          <!-- <el-input v-model="form.isfee" placeholder="请输入是否收费" /> -->
          <div>
            <el-radio :label="false" v-model="form.isfee">免费</el-radio>
            <el-radio :label="true" v-model="form.isfee">收费</el-radio>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listSection,
    listSectionb,
    getSection,
    delSection,
    addSection,
    updateSection,
    genAudio
  } from "@/api/book/section";
  import {
    getBook
  } from "@/api/book/book";
  export default {
    name: "Section",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 章节表格数据
        sectionList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          secname: null,
        },
        //书籍信息
        bookid: null,
        book: {},
        // 表单参数
        form: {
          isfee: false
        },
        // 表单校验
        rules: {
          secname: [{
            required: true,
            message: "章节名不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    created() {
      this.bookid = this.$route.params && this.$route.params.bookid;
      //console.log("bookid",bookid);
      this.getBook(this.bookid);
      this.getList();
    },
    methods: {
      getBook(bookid) {
        /**查询书籍*/
        getBook(bookid).then(response => {
          this.book = response.data;
        });
      },
      /** 查询章节列表 */
      getList() {
        this.loading = true;
        listSectionb(this.bookid, this.queryParams).then(response => {
          this.sectionList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          secname: null,
          ncontent: null,
          addtime: null,
          bookid: null,
          ordid: null,
          isfee: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加章节";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getSection(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改章节";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.bookid = this.bookid;
            if (this.form.id != null) {
              updateSection(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSection(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除章节编号为"' + ids + '"的数据项？').then(function() {
          return delSection(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      //生成音频
      handleGenerateAudio(row) {
        const ids = row.id || this.ids;

        this.$modal.confirm('是否确认重新生成编号为"' + ids + '"的音频？').then(function() {
          return genAudio(ids);
        }).then(response => {
          this.$modal.msgSuccess(response.msg);
        });

      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('book/section/export', {
          ...this.queryParams
        }, `section_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
